//
//  ThroughBusViewController.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/12/2.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "ThroughBusViewController.h"
#import "BuyTicketThroughBusCell.h"
#import "BuyTicketHeaderView.h"
#import "SelectAreaViewController.h"
#import "ThroughBusLineViewController.h"
#import "RegularBusViewModel.h"
#import "RoutePlanningViewController.h"
#import "CalendarViewController.h"
#import "KDateFormatter.h"
#import "AlipayManager.h"

@interface ThroughBusViewController () <SelectAreaViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *mainTable;
@property (strong, nonatomic) BuyTicketHeaderView *buyTicketHeaderView;

// 按钮的标记
@property (assign, nonatomic) NSInteger btnTag;

@property (strong, nonatomic) RegularBusViewModel *regularBusViewModel;
@property (strong, nonatomic) IBOutlet UIView *recommendSectionView;


@property (nonatomic , assign) NSInteger StartCityId;

@property (nonatomic , assign) NSInteger StartAreaId;

@property (nonatomic , assign) NSInteger EndCityId;

@property (nonatomic , assign) NSInteger EndAreaId;


@property (strong, nonatomic) ThroughBusLineViewModel *throughBusLineViewModel;
@end

@implementation ThroughBusViewController


-(void)awakeFromNib{
    self.throughBusLineViewModel = [[ThroughBusLineViewModel alloc ]init];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.buyTicketHeaderView = [BuyTicketHeaderView  getInstance];
    self.buyTicketHeaderView.alertStyle = 2;
    self.mainTable.tableHeaderView = self.buyTicketHeaderView;
    self.mainTable.backgroundColor = GLOBLE_BACKGROUND_COLOR;
    
    self.regularBusViewModel = [[RegularBusViewModel alloc ] init];

    WEAKSELF
    self.buyTicketHeaderView.BtnBlock = ^void(NSInteger btnTag){
        weakSelf.btnTag = btnTag;
        
        // 出发地或者目的地
        if (btnTag == 1 || btnTag == 2) {
            
            [weakSelf presentSelectLocation];
        }
        // 切换地点
        else if (btnTag == 3){
            
            NSInteger tmpCity = weakSelf.EndCityId;
            NSInteger tmpArea = weakSelf.EndAreaId;
          
            weakSelf.EndCityId  = weakSelf.StartCityId;
            weakSelf.EndAreaId  = weakSelf.StartAreaId;
            
            weakSelf.StartCityId  = tmpCity;
            weakSelf.StartAreaId  = tmpArea;
            
        }else if (btnTag == 4){
            
      
            [weakSelf searchLine];
        }
    };
}

-(void)searchLine{
    ThroughBusLineViewController *viewController = (ThroughBusLineViewController *)[ThroughBusLineViewController getViewControllerFromSbName:@"ThroughBusLine" viewControllerName:@"ThroughBusLineViewController"];
    viewController.throughBusLineViewModel.requestThroughBusLineEntity.StartCityId = self.StartCityId;
    viewController.throughBusLineViewModel.requestThroughBusLineEntity.StartAreaId = self.StartAreaId;
    
    viewController.throughBusLineViewModel.requestThroughBusLineEntity.EndCityId = self.EndCityId;
    viewController.throughBusLineViewModel.requestThroughBusLineEntity.EndAreaId = self.EndAreaId;
    [self.buyTicketsViewController.navigationController pushViewController:viewController animated:YES];
}


-(void)presentSelectLocation{
    UINavigationController *cityNavigation = (UINavigationController *)[BaseViewController getInitialViewControllerFromSbName:@"SelectArea"];
    
    SelectAreaViewController *sarea = cityNavigation.viewControllers[0];
    sarea.delegate = self;
    [self.buyTicketsViewController presentViewController:cityNavigation animated:YES completion:^{
        
    }];
}

-(void)didSelectedArea:(SelectedAreaModel *)cityModel right:(AreaEntity *)areaModel;
{
    WEAKSELF
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.buyTicketsViewController dismissViewControllerAnimated:YES completion:NULL];
        
        if (weakSelf.btnTag == 1) {
            weakSelf.StartCityId = cityModel.CityId;
            weakSelf.StartAreaId  = areaModel.AreaId;
            
            weakSelf.buyTicketHeaderView.startTextField.text = [NSString stringWithFormat:@"%@ %@",cityModel.CityName,areaModel.AreaName];
            
        }else if (self.btnTag == 2){
            weakSelf.EndCityId  = cityModel.CityId;
            weakSelf.EndAreaId  = areaModel.AreaId;
            weakSelf.buyTicketHeaderView.endTextField.text = [NSString stringWithFormat:@"%@ %@",cityModel.CityName,areaModel.AreaName];
        }
    });
    
   
}

-(void)loadNewRecommendData{
    self.regularBusViewModel.pageIndex = 1;
    [self loadData];
}

-(void)loadMoreRecommendData{
    self.regularBusViewModel.pageIndex ++;
    [self loadData];
}

-(void)loadData{
    WEAKSELF
    [self.regularBusViewModel getThroughBusRecommend:^(NSInteger resultType, id data, NSString *errorMsg) {
        if (resultType == 1 ) {
            
            weakSelf.mainTable.alpha = 1;
            [weakSelf.mainTable reloadData];
          
        }else{
            [SVProgressHUD showErrorWithStatus:errorMsg];
        }
        
        
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.regularBusViewModel.resultList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 63;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightFoFooterInSection:(NSInteger)section{
    return 0.01;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
   return [[UIView alloc ] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (self.regularBusViewModel.resultList.count > 0) {
        return self.recommendSectionView;
    }else{
        return [[UIView alloc ] initWithFrame:CGRectMake(0, 0, 0, 0)];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BuyTicketThroughBusCell *lineCell = [tableView dequeueReusableCellWithIdentifier:@"BuyTicketThroughBusCell"];
    RegularBusLineModel *entity = self.regularBusViewModel.resultList[indexPath.row];
    
    lineCell.startStation.text = entity.StartCityName;
    lineCell.endStation.text   = entity.EndCityName;
    
    return lineCell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RegularBusLineModel *regularBusLineModel = self.regularBusViewModel.resultList[indexPath.row];
    
    ThroughBusLineViewController *viewController = (ThroughBusLineViewController *)[ThroughBusLineViewController getViewControllerFromSbName:@"ThroughBusLine" viewControllerName:@"ThroughBusLineViewController"];
    viewController.throughBusLineViewModel.requestThroughBusLineEntity.StartCityId = regularBusLineModel.StartCityId;
    viewController.throughBusLineViewModel.requestThroughBusLineEntity.StartAreaId = 0;
    
    viewController.throughBusLineViewModel.requestThroughBusLineEntity.EndCityId = regularBusLineModel.EndCityId;;
    viewController.throughBusLineViewModel.requestThroughBusLineEntity.EndAreaId = 0;
    [self.buyTicketsViewController.navigationController pushViewController:viewController animated:YES];
    
   
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
